package com.sun.controller.admin;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sun.pojo.Type;
import com.sun.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author: glorysun
 * @date: 2020/8/5
 */
@Controller
@RequestMapping("/admin")
public class TypeController {

    @Autowired
    private TypeService typeService;

    //分类列表页
    @GetMapping("/types")
    public String list(Model model, @RequestParam(defaultValue = "1",value = "pageNum") Integer pageNum) {
        PageHelper.startPage(pageNum, 5);
        List<Type> typeList = typeService.selectTypeList();
        //得到分页结果对象
        PageInfo<Type> pageInfo = new PageInfo<>(typeList);
        model.addAttribute("pageInfo", pageInfo);
        return "admin/types";
    }

    //分类新增页
    @GetMapping("/types/input")
    public String toInput(){
        return "admin/types-input";
    }

    //保存新增
    @GetMapping("/types/inputSave")
    public String input(Type type, RedirectAttributes attributes){
        //判断分类名是否已存在
        Type type1=typeService.selectTypeByName(type.getName());
        if(type1==null){
            int i=typeService.insertType(type);
            if(i>0)
                attributes.addFlashAttribute("message","添加成功!");
            else
                attributes.addFlashAttribute("message","添加失败!");
            return "redirect:/admin/types";
        }else{
            attributes.addFlashAttribute("message","分类已存在!");
            return "redirect:/admin/types/input";
        }
    }

    //分类更新页
    @GetMapping("/types/edit/{id}")
    public String toEdit(@PathVariable Long id, HttpSession session){

        Type type = typeService.selectTypeById(id);
        session.setAttribute("type", type);
        return "admin/types-edit";
    }

    //保存更新
    @GetMapping("/types/editSave")
    public String edit(Type type,RedirectAttributes attributes,HttpSession session){
        Type type1=typeService.selectTypeByName(type.getName());
        if(type1==null){
            attributes.addFlashAttribute("message","更新成功!");
            session.removeAttribute("type");
            typeService.updateType(type);
            return "redirect:/admin/types";
        }else{
            session.setAttribute("message","分类已存在!");
            return "/admin/types-edit";
        }
    }

    //删除分类
    @GetMapping("/types/delete/{id}")
    public String delete(@PathVariable Long id,RedirectAttributes attributes){
        typeService.deleteType(id);
        attributes.addFlashAttribute("message","删除成功!");
        return "redirect:/admin/types";
    }
}
